package karileet.meeting.profile;

import android.content.Context;
import android.content.SharedPreferences;
import karileet.meeting.profile.model.ThirdParty;
import karileet.meeting.profile.model.Weekdays;

import java.util.Calendar;

/**
 * Class contains methods for retrieving/saving app states from/to shared
 * preferences
 *
 * @author markus karileet
 */
public class ProfileConf {
    public static final String PREFS_NAME = "MeetingProfilePrefsFile";
    public static String APP = "Meeting profile";
    public static String PROFILE = "profile";
    public static String VIBRATE = "vibrate";
    public static String START = "start";
    public static String STOP = "stop";
    public static String STARTENABLED = "startenabled";
    public static String STOPENABLED = "stopenabled";
    public static String BEEP = "beep";
    public static String STOPBEG = "beggar";
    public static String DAILYREMINDER = "beggarDaily";
    public static String WEEKDAYS = "weekdays";
    public static String THIRDPARTY = "thirdparty";
    public static String DISABLE = "Disable";
    public static String TOGGLE = "Toggle";
    public static String ACCESSIBILITY = "Accessibility";
    private Context ctx;

    /**
     * Constructor inits object and populates values from shared preferences
     * (when existing)
     *
     * @param ctx Context
     */
    public ProfileConf(Context ctx) {
        this.ctx = ctx;
    }

    /**
     * Method returns weekdays that profile should be enabled (workdays by default)
     *
     * @return Weekdays
     */
    public Weekdays getWeekdays() {
        Weekdays wd = new Weekdays();
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        wd.stringToWeekdays(p.getString(WEEKDAYS, "01234"));
        return wd;
    }

    /**
     * Method stores weekdays to preferences like 0123456
     *
     * @param wd Weekday object to save
     */
    public void setWeekdays(Weekdays wd) {
        editPreferences(WEEKDAYS, wd.toString());
    }

    /**
     * Method returns enabled third-party apps
     *
     * @return ThirdParty
     */
    public ThirdParty getThirdPartyApps() {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        String apps = p.getString(THIRDPARTY, "com.facebook.katana");
        ThirdParty tp = new ThirdParty();
        tp.stringToThirdParty(apps);
        return tp;
    }

    /**
     * Method stores third-party apps to preferences as a comma separated list of package names
     *
     * @param tp ThirdParty object to save
     */
    public void setThirdPartyApps(ThirdParty tp) {
        editPreferences(THIRDPARTY, tp.toString());
    }

    public boolean getAccessibilityEnabled() {
        return getBoolPreference(ACCESSIBILITY);
    }

    public void setAccessibilityEnabled(boolean accessibilityEnabled) {
        editPreferences(ACCESSIBILITY, accessibilityEnabled);
    }

    /**
     * @return int Volume of beep
     */
    public int getBeepVolume() {
        return getIntPreference(BEEP, 15);
    }

    /**
     * Method stores user defined beep volume
     *
     * @param beepVolume Volume of beep
     */
    public void setBeepVolume(int beepVolume) {
        editPreferences(BEEP, beepVolume);
    }

    /**
     * @return boolean whether app is enabled or not
     */
    public boolean isProfileEnabled() {
        return getBoolPreference(PROFILE);
    }

    /**
     * Method stores profile state to shared preferences
     *
     * @param profileEnabled Whether app is enabled or not
     */
    public void setProfileEnabled(boolean profileEnabled) {
        editPreferences(PROFILE, profileEnabled);
    }

    /**
     * @return boolean whether vibrate is enabled or not
     */
    public boolean isVibrateEnabled() {
        return getBoolPreference(VIBRATE);
    }

    /**
     * Method stores vibrate state to shared preferences
     *
     * @param vibrateEnabled Whether vibrate is enabled or not
     */
    public void setVibrateEnabled(boolean vibrateEnabled) {
        editPreferences(VIBRATE, vibrateEnabled);
    }

    /**
     * Method checks whether application is allowed to beg money from user
     *
     * @return boolean
     */
    public boolean isStopBegEnabled() {
        return getBoolPreference(STOPBEG);
    }

    /**
     * Method sets beg state to true or false
     */
    public void setStopBeg(boolean val) {
        editPreferences(STOPBEG, val);
    }

    /**
     * Method updates dailyreminder when 24 hours has passed. When update is
     * nessessary, true is returned. Otherwise returns false.
     *
     * @return Flag whether to show reminder or not
     */
    public boolean setDailyReminder() {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        long oneDay = 86400000;
        boolean retval = false;
        Calendar AlarmCal = Calendar.getInstance();
        AlarmCal.setTimeInMillis(p.getLong(DAILYREMINDER, 0));

        //adjust start time, so that service wont start to past time
        if ((AlarmCal.getTimeInMillis() + oneDay) < System.currentTimeMillis()) {

            p.edit().putLong(DAILYREMINDER, System.currentTimeMillis()).commit();
            retval = true;
        }
        return retval;
    }

    /**
     * Method commits state under type to sharedPreferences
     *
     * @param type  Preference name
     * @param state State to save
     */
    private void editPreferences(String type, boolean state) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        p.edit().putBoolean(type, state).commit();
    }

    /**
     * Method commits state under type to sharedPreferences
     *
     * @param type  Preference name
     * @param value State to save
     */
    private void editPreferences(String type, String value) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        p.edit().putString(type, value).commit();
    }

    /**
     * Method commits state under type to sharedPreferences
     *
     * @param type  Preference name
     * @param value State to save
     */
    private void editPreferences(String type, int value) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        p.edit().putInt(type, value).commit();
    }

    private int getIntPreference(String type, int defaultValue) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        return p.getInt(type, defaultValue);
    }

    private boolean getBoolPreference(String type) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        return p.getBoolean(type, false);
    }

    public long getScheduleStart() {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        return p.getLong(START, 0);
    }

    public void setScheduleStart(long start) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        p.edit().putLong(START, start).commit();
    }

    public long getScheduleStop() {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        return p.getLong(STOP, 0);
    }

    public void setScheduleStop(long stop) {
        SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
        p.edit().putLong(STOP, stop).commit();
    }

    public boolean isScheduleStartEnabled() {
        return getBoolPreference(STARTENABLED);
    }

    public void setScheduleStartEnabled(boolean startEnabled) {
        editPreferences(STARTENABLED, startEnabled);
    }

    public boolean isScheduleStopEnabled() {
        return getBoolPreference(STOPENABLED);
    }

    public void setScheduleStopEnabled(boolean stopEnabled) {
        editPreferences(STOPENABLED, stopEnabled);
    }
}